//
//  STCollectApi.swift
//  Story
//
//  Created by 汤泽川 on 2019/5/4.
//  Copyright © 2019 汤泽川. All rights reserved.
//

import Foundation
class STCollectApi {
    static public func queryCollectState(storyId : Int, result : @escaping (_ response : ResponseModel<Dictionary<String, Int> >?, _ error : STError?) -> Void) {
        let request = STRequest<ResponseModel<Dictionary<String, Int> > >()
        request.requestPath = "/story/query_collection_state"
        request.requestParam = ["storyId" : storyId]
        request.sendRequest { (state) in
            switch state {
            case .success(let data):
                if data.isSuccess {
                    result(data, nil)
                } else {
                    result(nil, STError(code: data.status, reason: data.message, errorType: .customError))
                }
            case .fail(let error):
                result(nil, error)
            }
        }
    }
    
    static public func changeCollectState(storyId : Int, state : Bool, result : @escaping (_ error : STError?) -> Void) {
        let request = STRequest<ResponseModel<String> >()
        request.requestPath = "/story/collection"
        request.requestParam = ["storyId" : storyId, "state" : (state ? 1 : 0)]
        request.sendRequest { (state) in
            switch state {
            case .success(let data):
                if data.status != 0 {
                    result(STError(code: data.status, reason: data.message, errorType: .customError))
                } else {
                    result(nil)
                }
            case .fail(let error):
                result(error)
            }
        }
    }
    
    static public func quaryCollectStorys(page : Int, count : Int, result : @escaping (_ response : ResponseModel<[STStoryInfo]>?, _ error : STError?) -> Void) {
        let request = STRequest<ResponseModel<[STStoryInfo]> >()
        request.requestPath = "/story/query_collection_story"
        request.requestParam = ["page" : page, "count" : count]
        request.sendRequest { (state) in
            switch state {
            case .success(let data):
                if data.status != 0 {
                    result(nil, STError(code: data.status, reason: data.message, errorType: .customError))
                } else {
                    result(data, nil)
                }
            case .fail(let error):
                result(nil, error)
            }
        }
    }
}
